﻿using BackendLogic.BaseLogic;
using ComTools.EnumHelper;
using ComTools.Helpers;
using ComTools.Mapping;
using LogicModel.DataModel;
using LogicModel.ViewModel;
using MappingDTO.Admin;
using MSSQL.CommandTool;
using MSSQL.Enums;
using MSSQL.PagingCorrelation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ToolsModel;

namespace BackendLogic
{
    /// <summary>
    /// 管理员操作逻辑类
    /// </summary>
    public class SysUserServiceImpl : BaseLogicService, ISysUserService
    {
        /// <summary>
        /// 用户登录
        /// </summary>
        /// <returns></returns>
        public ResultJsonInfo<AdminTicket> UserLogin(LoginReqDTO userInfo)
        {
            var resultInfo = new ResultJsonInfo<AdminTicket>();

            if (userInfo.sLoginName != null)
            {
                query.DataBaseName = DatabaseName.GeneralFrameworkDB.GetEnumItemDescription();

                var info = query.QuerySingleByWhere<SysUserEntity>(DataCommandManager.GetSQLCommand("CheckLoginName", "SysUser"),
                    new
                    {
                        sLoginName = userInfo.sLoginName
                    });
                if (info != null && info.LoginName != null)
                {
                    var result = query.QuerySingleData<View_SysUserRoleView>(DataCommandManager.GetSQLCommand("CheckLogin", "SysUser"),
                                 new
                                 {
                                     sLoginName = userInfo.sLoginName,
                                     sPassword = SecurityHelper.GetMD5Hash(userInfo.sPassword)
                                 });
                    if (result != null)
                    {
                        if (result.State == (int)UserState.Normal)
                        {
                            var ticket = result.MapTo<AdminTicket>();
                            resultInfo.Code = 1;
                            resultInfo.Msg = "操作成功";
                            resultInfo.Data = ticket;
                        }
                        else
                        {
                            resultInfo.Msg = "用户已被冻结，请与管理员联系！";
                        }
                    }
                    else
                    {
                        resultInfo.Msg = "密码错误！";
                    }
                }
                else
                {
                    resultInfo.Msg = "账号不存在！";
                }
            }
            return resultInfo;
        }

        /// <summary>
        /// 根据唯一标识符获取用户信息
        /// </summary>
        /// <param name="sCode"></param>
        /// <returns></returns>
        public ResultJsonInfo<SysUserRetDTO> LoadUserByCode(string sCode)
        {

            var resultInfo = new ResultJsonInfo<SysUserRetDTO>();

            query.DataBaseName = DatabaseName.GeneralFrameworkDB.GetEnumItemDescription();
            var info = query.QuerySingleData<SysUserEntity>(DataCommandManager.GetSQLCommand("LoadUserByCode", "SysUser"),
                new { sCode = sCode });
            if (info != null)
            {
                resultInfo.Code = 1;
                resultInfo.Msg = "操作成功";
                resultInfo.Data = info.MapTo<SysUserRetDTO>();
            }
            else
            {
                resultInfo.Msg = "无该用户信息！";
            }
            return resultInfo;
        }

        /// <summary>
        /// 获取所有有效用户列表
        /// </summary>
        /// <returns></returns>
        public ResultJsonInfo<IList<SysUserRetDTO>> LoadAllUserList()
        {
            var resultInfo = new ResultJsonInfo<IList<SysUserRetDTO>>();
            query.DataBaseName = DatabaseName.GeneralFrameworkDB.GetEnumItemDescription();
            var info = query.QueryListData<SysUserEntity>(DataCommandManager.GetSQLCommand("LoadAllUserList", "SysUser"), null);
            if (info != null)
            {
                resultInfo.Code = 1;
                resultInfo.Msg = "操作成功";
                resultInfo.Data = info.MapToList<SysUserRetDTO>();
            }
            else
            {
                resultInfo.Msg = "无用户信息！";
            }
            return resultInfo;
        }


        ///// <summary>
        /////  获取分页用户列表
        ///// </summary>
        ///// <returns></returns>
        //public ResultJsonInfo<IList<SysUserEntity>> LoadUserList(ParametersInfo parametersInfo)
        //{
        //    var result = new ResultJsonInfo<IList<SysUserEntity>>();
        //    query.DataBaseName = DatabaseName.MMYoJaDB.GetEnumItemDescription();

        //    PageInfo pageInfo = new PageInfo()
        //    {
        //        PageIndex = parametersInfo.pageIndex,
        //        PageSize = parametersInfo.pageSize,
        //        OrderBy = parametersInfo.sortName,
        //        orderType = parametersInfo.sortOrder == "asc" ? OrderType.ASC : OrderType.DESC
        //    };

        //    StringBuilder whereStr = new StringBuilder();

        //    if (!string.IsNullOrWhiteSpace(parametersInfo.parameters["sLoginName"].ToString()))
        //    {
        //        whereStr.Append(" AND sLoginName  LIKE '%" + parametersInfo.parameters["sLoginName"].ToString() + "%'");
        //    }

        //    if (!string.IsNullOrWhiteSpace(parametersInfo.parameters["gRoleCode"].ToString()))
        //    {
        //        whereStr.Append(" AND  gRoleCode = '" + parametersInfo.parameters["gRoleCode"].ToString() + "'");
        //    }

        //    var resultInfo = query.QueryPaginationData<SysUserEntity>(pageInfo, string.Format(DataCommandManager.GetSQLCommand("LoadUserList", "SysUser"), whereStr), null);

        //    if (resultInfo.Result.Count > 0)
        //    {
        //        result.Code = 1;
        //        result.Data = resultInfo.Result;
        //        result.MaxCount = resultInfo.MaxCount;
        //    }
        //    else
        //    {
        //        result.Msg = "无对应信息！";
        //    }
        //    return result;
        //}

        ///// <summary>
        ///// 保存用户信息
        ///// </summary>
        ///// <param name="parametersInfo"></param>
        ///// <returns></returns>
        //public ResultJsonInfo<int> SaveUserInfo(ParametersInfo parametersInfo)
        //{
        //    var result = new ResultJsonInfo<int>();

        //    excute.DataBaseName = DatabaseName.MMYoJaDB.GetEnumItemDescription();
        //    query.DataBaseName = DatabaseName.MMYoJaDB.GetEnumItemDescription();

        //    SysUserEntity optInfo = parametersInfo.parameters.ObjectToJSON().JSONToObject<SysUserEntity>();
        //    int resultCount = 0;

        //    //修改
        //    if (parametersInfo.parameters.ContainsKey("gCode") && !string.IsNullOrEmpty(parametersInfo.parameters["gCode"].ToString()))
        //    {
        //        resultCount = excute.UpdateSingle<SysUserEntity>(optInfo);
        //    }
        //    //添加
        //    else
        //    {
        //        optInfo.gCode = GuidHelper.GetGuid();
        //        optInfo.sPassword = SecurityHelper.GetMD5Hash(optInfo.sPassword);
        //        resultCount = excute.InsertSingle<SysUserEntity>(optInfo);
        //    }
        //    if (resultCount > 0)
        //    {
        //        result.Code = 1;
        //        result.Data = resultCount;
        //        result.Msg = "保存信息成功！";
        //    }
        //    else
        //    {
        //        result.Msg = "保存信息失败！";
        //    }
        //    return result;
        //}


        ///// <summary>
        ///// 保存管理员密码信息
        ///// </summary>
        ///// <param name="parametersInfo"></param>
        ///// <returns></returns>
        //public ResultJsonInfo<int> SaveUserPswInfo(ParametersInfo parametersInfo)
        //{
        //    var result = new ResultJsonInfo<int>();
        //    excute.DataBaseName = DatabaseName.MMYoJaDB.GetEnumItemDescription();
        //    query.DataBaseName = DatabaseName.MMYoJaDB.GetEnumItemDescription();

        //    string gCode = parametersInfo.parameters["gCode"].ToString();
        //    string sOldPsw = SecurityHelper.GetMD5Hash(parametersInfo.parameters["sOldPsw"].ToString());
        //    string sNewPsw = SecurityHelper.GetMD5Hash(parametersInfo.parameters["sNewPsw"].ToString());
        //    int resultCount = 0;
        //    var userInfos = query.QueryListData<Dictionary<string, object>>(DataCommandManager.GetSQLCommand("CheckLoginByCode", "SysUser"),
        //        new
        //        {
        //            gCode = gCode,
        //            sPassword = sOldPsw
        //        });

        //    if (userInfos != null && userInfos.Count > 0)
        //    {
        //        SysUserEntity sysUser = new SysUserEntity();
        //        sysUser.gCode = gCode;
        //        sysUser.sPassword = sNewPsw;
        //        resultCount = excute.UpdateSingle<SysUserEntity>(sysUser);
        //        if (resultCount > 0)
        //        {
        //            result.Code = 1;
        //            result.Data = resultCount;
        //            result.Msg = "操作成功！";
        //        }
        //        else
        //        {
        //            result.Msg = "操作失败！";
        //        }
        //    }
        //    else
        //    {
        //        result.Code = 0;
        //        result.Msg = "旧密码不正确！";
        //    }
        //    return result;
        //}


        ///// <summary>
        ///// 删除管理员信息
        ///// </summary>
        ///// <param name="parametersInfo"></param>
        ///// <returns></returns>
        //public ResultJsonInfo<int> Remove(ParametersInfo parametersInfo)
        //{
        //    var result = new ResultJsonInfo<int>();
        //    excute.DataBaseName = DatabaseName.MMYoJaDB.GetEnumItemDescription();
        //    query.DataBaseName = DatabaseName.MMYoJaDB.GetEnumItemDescription();
        //    if (parametersInfo.parameters.ContainsKey("gCode") && !string.IsNullOrEmpty(parametersInfo.parameters["gCode"].ToString()))
        //    {
        //        string gCode = parametersInfo.parameters["gCode"].ToString();

        //        var regionVillage = query.QueryListData<Dictionary<string, object>>(DataCommandManager.GetSQLCommand("LoadByManagerCode", "RegionVillage"),
        //            new
        //            {
        //                gManagerCode = gCode
        //            });

        //        if (regionVillage != null && regionVillage.Count > 0)
        //        {
        //            result.Code = 0;
        //            result.Msg = "管理员有负责小区，无法删除！";
        //        }
        //        else
        //        {
        //            var regionArea = query.QueryListData<Dictionary<string, object>>(DataCommandManager.GetSQLCommand("LoadByManagerCode", "RegionArea"),
        //                   new
        //                   {
        //                       gManagerCode = gCode
        //                   });
        //            if (regionArea != null && regionArea.Count > 0)
        //            {
        //                result.Code = 0;
        //                result.Msg = "管理员有负责区域，无法删除！";
        //            }
        //            else
        //            {

        //                int resultCount = excute.UpdateBySQL(DataCommandManager.GetSQLCommand("RemoveUserInfo", "SysUser"),
        //                   new
        //                   {
        //                       gCode = gCode
        //                   }); ;
        //                if (resultCount > 0)
        //                {
        //                    result.Code = 1;
        //                    result.Data = resultCount;
        //                    result.Msg = "操作成功！";
        //                }
        //                else
        //                {
        //                    result.Msg = "操作失败！";
        //                }
        //            }
        //        }
        //    }
        //    return result;
        //}
    }
}
